package com.sxz.sbs.module.mine;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.sxz.sbs.R;
import com.sxz.sbs.app.App;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.component.ClearEditText;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.mine.presenter.ChangePasswordPresenter;
import com.sxz.sbs.module.mine.view.ChangePasswordView;
import com.sxz.sbs.widget.CommonTitle;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hp
 * @date 2018/1/8
 * 修改密码
 */

public class ChangePasswordActivity extends BaseActivity<ChangePasswordPresenter> implements ChangePasswordView {

    @BindView(R.id.tv_account_name)
    TextView tvAccountName;
    @BindView(R.id.cet_old_psw)
    ClearEditText cetOldPsw;
    @BindView(R.id.cet_new_psw)
    ClearEditText cetNewPsw;
    @BindView(R.id.cet_sure_new_psw)
    ClearEditText cetSureNewPsw;


    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("修改密码").build().show();
        String phone = getIntent().getStringExtra("user_phone");
        tvAccountName.setText(phone);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_changepassword;
    }

    @Override
    protected ChangePasswordPresenter createPresenter() {
        return new ChangePasswordPresenter();
    }

    @OnClick({R.id.tv_changepassword})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_changepassword:
                String old = cetOldPsw.getInputValue();
                if (TextUtils.isEmpty(old)) {
                    cetOldPsw.setShakeAnimation();
                    return;
                }
                String newPsw = cetNewPsw.getInputValue();
                if (TextUtils.isEmpty(newPsw)) {
                    cetNewPsw.setShakeAnimation();
                    return;
                }
                String sureNewPsw = cetSureNewPsw.getInputValue();
                if (TextUtils.isEmpty(sureNewPsw)) {
                    cetSureNewPsw.setShakeAnimation();
                    return;
                }
                if (!newPsw.equals(sureNewPsw)) {
                    ToastUtils.showShort("两次密码输入不一致");
                    return;
                }
                HashMap<String,Object> map = HttpParamMap.getParamMap();
                map.put("oldPassword",old);
                map.put("newPassword",sureNewPsw);
                mPresenter.alterPsw(map);
                break;
            default:
                break;
        }
    }


    @Override
    public void onAlterPasswordSuccess() {
        App.preferences.setValue(AppConstant.Preference.USER_PSW, cetSureNewPsw.getInputValue());
        ToastUtils.showShort("修改成功");
        finish();
    }
}
